package com.pro.service.imple;

import com.pro.mapper.CouponMapper;
import com.pro.mapper.CouponUsedMapper;
import com.pro.pojo.Coupon;
import com.pro.pojo.CouponUsed;
import com.pro.pojo.JsBeforeUser;
import com.pro.service.CouponUsedService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * auther 杨沛琪
 * createDate 2022/1/12
 */
@Service
public class CouponUsedServiceImple implements CouponUsedService {
    @Autowired
    private CouponUsedMapper couponUsedMapper;
    @Autowired
    private CouponMapper couponMapper;

    //获取用户优惠券使用记录
    @Override
    public List<JsBeforeUser> getTable() {
        return couponUsedMapper.getTable();
    }

    //根据账号和昵称筛选查询
    @Override
    public List<CouponUsed> queryCouponUsed(String account, String nickname) {
        return couponUsedMapper.queryCouponUsed(account, nickname);
    }

    //查询用户优惠券使用记录
    @Override
    public List<String> getTableByAnUsedState(Integer userId, Integer usedState) {
        return couponUsedMapper.getTableByAnUsedState(userId, usedState);
    }

    //查询已过期
    @Override
    public List<String> getTableByEndDate(Integer userId) {
        return couponUsedMapper.getTableByEndDate(userId);
    }

    //添加优惠券使用记录
    @SneakyThrows()
    @Override
    public boolean addUserCouponUsed(CouponUsed couponUsed, Coupon coupon) throws Exception {
        couponUsed.setUsedState(1);
        couponUsed.setGetDate(String.valueOf(new Timestamp(new Date().getTime())));
        boolean addUserCouponUsed = couponUsedMapper.addUserCouponUsed(couponUsed);
        Boolean b = null;
        if(addUserCouponUsed){
            coupon.setUpdateDate(new Date());
            b = couponMapper.updateUserCoupon(coupon);
        }else{
            throw  new Exception("添加失败");
        }
        return b;
    }
}
